<?php
/**
 * Notes:
 * Created by PhpStorm.
 * Author: TS
 * Date: 2019/11/22
 * Time: 21:52
 */

namespace app\auth\controller;


use app\model\Auth as AuthModel;
use app\model\Rule;
use think\Exception;

class Auth
{
    public function checkLogin(){
        if(session("contact_user") == null) return res(0,'请登录');
        return res(1,'已登录');
    }
    public function checkAuth(){
        $role_id = $user = json_decode(session("contact_user"),true)["role_id"];
        if(!checkNull($role_id)) return res(0,'请登录');
        $rule_key = input('rule_key');
        if(!checkNull($rule_key)) return res(0,'错误标识');
        $RuleModel = new Rule();
        $AuthModel = new AuthModel();
        try{
            $Rule = $RuleModel->checkKey($rule_key);
            if(!$Rule) return res(0,'无该规则');
            $check_res = $AuthModel->check($role_id,$Rule["rule_id"]);
            if($check_res){
                return res(1,'允许');
            }
            return res(0,'抱歉，您无权限');
        }catch (Exception $e){
            return res(0,$e);
        }

    }

}